<?php

class ReportController extends Zend_Controller_Action {
	
	public function saleAction() {
		
		if ($this->getRequest ()->isPost ()) {
			
			$product = $this->_getParam ( 'product' );
			
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
			
			$fromDate = null;
			$toDate = null;
				
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
				
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
			
			$saleModel = new Cms_Model_Sale ();
			
			$salesReport = $saleModel->getSalesReport ( $product, $fromDate, $toDate );
			
			// filename for download
			$filename = "sales-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
			
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
			
			echo  "Date Sold" . "\t" ."Sale ID" . "\t"  . "Product" . "\t" . "Quantity" . "\t" . "Vat" . "\t" . "Discount" . "\t" . 
				 "Unit Price" . "\t" . "Total" . "\t" . "Sold By" . "\t" . "Date Sold" . "\t" . 
				 "Customer Name" . "\t" . "Customer Phone No" . "\n";
			
			foreach ( $salesReport as $salesReportData ) {
				echo date ( 'd-M-Y', strtotime ( $salesReportData->dateSold ) ) . "\t" . $salesReportData->saleId . "\t" . $salesReportData->product . "\t" . $salesReportData->quantity . "\t" . 
					 $salesReportData->tax . "\t" . $salesReportData->discount . "\t" . $salesReportData->unitPrice . "\t" . 
					 $salesReportData->total . "\t" . $salesReportData->soldBy . "\t" .
					 $salesReportData->customerName . "\t" . $salesReportData->customerPhone . "\n";
			}
			
			exit ( 0 );
		
		}
		
		$catalogModel = new Cms_Model_Catalog ();
		
		$this->view->products = $catalogModel->getProductsByCategory ( 0, array ('sort' => 'name' ) );
	
	}
	
	public function inventoryAction() {
		
		if ($this->getRequest ()->isPost ()) {
			
			$product = $this->_getParam ( 'product' );
			
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
			
		 	$fromDate = null;
			$toDate = null;
				
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
				
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
			
			$inventoryModel = new Cms_Model_Inventory ();
			
			$inventoryReport = $inventoryModel->getInventoryReport ( $product, $fromDate, $toDate );
			
			// filename for download
			$filename = "purchase-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
			
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
			
			echo "Order Date" . "\t" . "Entry ID" . "\t" . "Product Name" . "\t" . "Quantity" . "\t" . "Supplier Name" . "\t" .
				 "Added By" . "\t" . "Date Added" . "\n";
			
			foreach ( $inventoryReport as $inventoryReportData ) {
				
				echo $inventoryReportData->orderDate . "\t" . $inventoryReportData->entryId . "\t" . $inventoryReportData->name . "\t" . $inventoryReportData->quantity . "\t" . 
					 $inventoryReportData->supplier . "\t" . $inventoryReportData->addedBy . "\t" . 
					 $inventoryReportData->addedDate . "\n";
			
			}
			
			exit ( 0 );
		
		}
		
		$catalogModel = new Cms_Model_Catalog ();
		
		$this->view->products = $catalogModel->getProductsByCategory ( 0, array ('sort' => 'name' ) );
	
	}
	
	public function expenseAction() {
		
		if ($this->getRequest ()->isPost ()) {
			
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
			
			$fromDate = null;
			$toDate = null;
			
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
			
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
			
			$expenseModel = new Cms_Model_Expense ();
			
			$expenseReport = $expenseModel->getExpenseReport ( $fromDate, $toDate );
			
			// filename for download
			$filename = "expense-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
			
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
			
			echo "Expense Date" . "\t" . "Entry ID" . "\t" . "Description" . "\t" . "Amount" . "\t" . "Added By" . "\t" . "Date Added" . "\n";
			
			foreach ( $expenseReport as $expenseReportData ) {
				echo date ( 'd-M-Y', strtotime ( $expenseReportData->expenseDate ) ) . "\t" . $expenseReportData->entryId . "\t" . 
					 $expenseReportData->description . "\t" . $expenseReportData->amount . "\t" . $expenseReportData->addedBy . "\t" . 
					 date ( 'd-M-Y h:i:s', strtotime ( $expenseReportData->addedDate ) ) . "\n";
			}
			
			exit ( 0 );
		}
	
	}
	
	public function payrollAction() {
		
		if ($this->getRequest ()->isPost ()) {
			
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
			
			$fromDate = null;
			$toDate = null;
			
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
			
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
			
			$StaffModel = new Cms_Model_Staff ();
			
			$payrollReport = $StaffModel->getPayrollReport ( $fromDate, $toDate );
			
			// filename for download
			$filename = "payroll-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
			
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
			
			echo "Period" . "\t" . "Entry ID" . "\t" . "Staff Name" . "\t" . "Staff Position" . "\t" . "Allowance" . "\t" . 
				 "Basic Salary" . "\t" . "Tax" . "\t" . "Date Paid" . "\t" . "Added By" . "\t" . "Date Added" . "\n";
			
			foreach ( $payrollReport as $payrollReportData ) {
				echo $payrollReportData->period . "\t" .$payrollReportData->entryId . "\t" . $payrollReportData->name . "\t" . 
					 $payrollReportData->staffPosition . "\t" . $payrollReportData->allowance . "\t" . $payrollReportData->basicSalary . "\t" . 
					 $payrollReportData->tax . "\t" . date ( 'd-M-Y', strtotime ( $payrollReportData->datePaid ) ) . "\t" . 
					 $payrollReportData->addedBy . "\t" . date ( 'd-M-Y h:i:s', strtotime ( $payrollReportData->addedDate ) ) . "\n";
			}
			
			exit ( 0 );
		}
	
	}

	public function assetAction() {
	
		if ($this->getRequest ()->isPost ()) {
				
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
				
			$fromDate = null;
			$toDate = null;
				
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
				
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
				
			$catalogModel = new Cms_Model_Catalog ();
				
			$assetReport = $catalogModel->getAssetReport ( $fromDate, $toDate );
				
			// filename for download
			$filename = "asset-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
				
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
				
			echo "Purchased Date" . "\t" . "Asset ID" . "\t" . "Name" . "\t" . "Description" . "\t" . "Cost" . "\t" . "Added By" . "\t" . "Date Added" . "\n";
				
			foreach ( $assetReport as $assetReportData ) {
				echo date ( 'd-M-Y', strtotime ( $assetReportData->purchasedDate ) ) . "\t" . $assetReportData->assetId . "\t" . $assetReportData->name . "\t" . 
					 $assetReportData->description . "\t" . $assetReportData->cost . "\t" . $assetReportData->addedBy . "\t" . 
					 date ( 'd-M-Y h:i:s', strtotime ( $assetReportData->addedDate ) ) . "\n";
			}
				
			exit ( 0 );
		}
	
	}
	
	public function staffAction() {
	
		if ($this->getRequest ()->isPost ()) {
				
			$startDate = $this->_getParam ( 'startDate' );
			$endDate = $this->_getParam ( 'endDate' );
				
			$fromDate = null;
			$toDate = null;
				
			if (! empty ( $startDate )) {
				$startDate = new Zend_Date ( $startDate, 'dd.MM.yyyy' );
				$fromDate = $startDate->toString ( 'yyyy-MM-dd' );
			}
				
			if (! empty ( $endDate )) {
				$endDate = new Zend_Date ( $endDate, 'dd.MM.yyyy' );
				$toDate = $endDate->toString ( 'yyyy-MM-dd' );
			}
				
			$staffModel = new Cms_Model_Staff ();
				
			$staffReport = $staffModel->getStaffReport( $fromDate, $toDate );
				
			// filename for download
			$filename = "staff-report" . ".xls";
			$this->_helper->layout->disableLayout ();
			$this->_helper->viewRenderer->setNoRender ( true );
				
			$this->getResponse ()->setHeader ( "Content-Disposition", "attachment; filename=$filename" );
			$this->getResponse ()->setHeader ( 'Content-Type', 'application/vnd.ms-excel' );
			$this->getResponse ()->sendHeaders ();
				
			echo "Entry Date" . "\t" . "Staff ID" . "\t" . "Staff Name" . "\t" . "Staff Position" . "\t" . "Address" . "\t" . "Phone" . "\t" . "Added By" . "\n";
				
			foreach ( $staffReport as $staffReportData ) {
				echo date ( 'd-M-Y', strtotime ( $staffReportData->addedDate ) ) . "\t" . $staffReportData->staffId . "\t" .
						$staffReportData->name . "\t" . $staffReportData->position . "\t" . $staffReportData->address . "\t" .
						$staffReportData->phone . "\t" .$staffReportData->addedBy . "\n";
			}
				
			exit ( 0 );
		}
	
	}
}